package py.una.pol.pweb.compraventa;

import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import py.una.pol.pweb.compraventa.modelo.Cliente;
import py.una.pol.pweb.compraventa.modelo.Pago;

/**
 *
 * @author cvalenzuela
 */
@ManagedBean
@SessionScoped
public class PagoBean {

    private Pago pago = new Pago();
    private Cliente cliente = new Cliente();
    private Cliente clienteSelected=new Cliente();

    @SuppressWarnings("unchecked")
    public List<Cliente> getAllClientes() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompraVentaPU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        Query allClientesQuery = em.createNamedQuery("Cliente.findAll");
        List<Cliente> allClientes = allClientesQuery.getResultList();

        tx.commit();
        em.close();
        cliente = new Cliente();
        return allClientes;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Cliente getClienteSelected() {
        return clienteSelected;
    }

    public void setClienteSelected(Cliente clienteSelected) {
        this.clienteSelected = clienteSelected;
    }

    public Pago getPago() {
        return pago;
    }

    public void setPago(Pago pago) {
        this.pago = pago;
    }

    public void limpiarForm() {
        clienteSelected = new Cliente();
        pago = new Pago();
    }

    /*guargar la relacion cliente pago*/
    public void guardarPago() {

        Double saldo;
        saldo = clienteSelected.getSaldo()-pago.getMonto();
        FacesContext context = FacesContext.getCurrentInstance();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompraVentaPU");
        EntityManager em = emf.createEntityManager();
        try {

            em.getTransaction().begin();
            
            em.persist(pago);
           
            Cliente userx = em.find(Cliente.class, clienteSelected.getIdcliente());
            userx.setNombre(clienteSelected.getNombre());
            userx.setDireccion(clienteSelected.getDireccion());
            userx.setTelefono(clienteSelected.getTelefono());
            userx.setSaldo(saldo);
            userx.addPago(pago);
            em.getTransaction().commit();
            clienteSelected=new Cliente();
            pago = new Pago();
           
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Exito!", userx.getNombre()+", pago realizado corectamente.");
            context.addMessage(null, msg);

        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            em.close();
        }
    }
}
